<template>
  <el-dialog :title="!dataForm.id? '新增商品分类' : '修改商品分类'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px">
      <el-form-item label="上级分类" prop="parentId" placeholder="顶级">
        <el-popover
          ref="goodsCatsPopover"
          placement="bottom-start"
          trigger="click" :disabled="this.dataForm.id != null">
          <el-tree
            :data="goodsCats"
            :props="goodsCatsTreeProps"
            node-key="id"
            ref="goodsCatsTree"
            @current-change="goodsCatsTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input v-model="dataForm.parentName"
          v-popover:goodsCatsPopover
          :readonly="true"
          placeholder="点击选择上级分类"
          class="menu-list__input"
        ></el-input>
      </el-form-item>
      <el-form-item label="分类名称" prop="catName">
        <el-input v-model="dataForm.catName" placeholder="分类名称"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :disabled="disabled" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { treeDataTranslate } from '@/utils'
  export default {
    props:{
      companyId: {
        type: Number,
        default: function(){
          return null
        }
      }
    },
    data() {
      return {
        visible: false,
        disabled: false,
        dataForm: {
          id: null,
          parentId: 0,
          companyId: this.companyId,
          catName: null,
          parentName: ''
        },
        goodsCats: [],
        goodsCatsTreeProps: {
          label: 'catName',
          children: 'children'
        },
        dataRule: {
          catName: [{
            required: true,
            message: '分类名称不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || null;
        this.$http({
          url: this.$http.adornUrl('/merchant/goodsCat/selectByCompany'),
          method: 'get',
          params: this.$http.adornParams({
            companyId: this.companyId
          })
        }).then(({data}) => {
          this.goodsCats = treeDataTranslate(data.data)
        }).then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields();
          })
        }).then(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/merchant/goodsCat/detail/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.dataForm.parentId = data.item.parentId;
                this.dataForm.companyId = data.item.companyId;
                this.dataForm.catName = data.item.catName;
                this.goodsCatTreeSetCurrentNode()
              }
            })
          } else {
            this.goodsCatTreeSetCurrentNode()
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.disabled = true;
            this.$http({
              url: this.$http.adornUrl(`/merchant/goodsCat/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                id: this.dataForm.id || undefined,
                parentId: this.dataForm.parentId,
                companyId: this.dataForm.companyId,
                catName: this.dataForm.catName
              })
            }).then(({
              data
            }) => {
              this.disabled = false;
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit('refreshDataList');
                  }
                })
              } else {
                this.$message.error(data.msg);
              }
            })
          }
        })
      },
      // 分类树选中
      goodsCatsTreeCurrentChangeHandle(data, node){
        this.dataForm.parentId = data.id
        this.dataForm.parentName = data.catName
      },
      goodsCatTreeSetCurrentNode(){
        this.$refs.goodsCatsTree.setCurrentKey(this.dataForm.parentId)
        this.dataForm.parentName = (this.$refs.goodsCatsTree.getCurrentNode() || {})['catName']
      }
    }
  }
</script>
